package com.cdream.basic.algorithm.leetcode.tree;

public class Question108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return buildTree(nums, 0, nums.length - 1);
    }

    private TreeNode buildTree(int[] nums, int left, int right) {
        if (right < left) {
            return null;
        }
        int midIndex = left + (right - left) / 2;
        TreeNode leftNode = buildTree(nums, left, midIndex - 1);
        TreeNode rightNode = buildTree(nums, midIndex + 1, right);
        return new TreeNode(nums[midIndex], leftNode, rightNode);
    }
}
